<?php 

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Short description for file
 *
 * Long description for file (if any)...
 *
 * PHP version 5
 *
 * Copyright 2008 The Picnic PHP Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @category      Framework
 * @package       Pfw
 * @author        Sean Sitter <sean@picnicphp.com>
 * @copyright     2008 The Picnic PHP Framework
 * @license       http://www.apache.org/licenses/LICENSE-2.0
 * @link          http://www.picnicphp.com
 * @since         0.10
 */

class Pfw_Request_Standard {
    protected $method;
    protected $params;
    
    public function __construct()
    {
        $this->params =& $_REQUEST;
        $this->method = strtoupper($_SERVER['REQUEST_METHOD']);
    }
    
    public function getHttpMethod() 
    {
        return $this->method;
    }

    public function setParams($params)
    {
        $this->params = array_merge($this->params, $params);
    }
    
    public function setParam($name, $value)
    {
        $this->params[$name] = $value;
    }
    
    public function getParams()
    {
        return $this->params;
    }
    
    public function getParam($name)
    {
        return (isset($this->params[$name])) ? $this->params[$name] : null;
    }
    
    public function getEntityBody()
    {
        // do input stream stuff here
        return file_get_contents('php://input');
    }
    
    public function _setHttpMethod($method)
    {
        $this->method = strtoupper($method);
    }
    
    public function isHttpPost()
    {
        return ($this->getHttpMethod() == "POST") ? true : false;
    }
    
    public function isAjax()
    {
        return ($this->getHeaderParam('x-requested-with') == 'XMLHttpRequest') ? true : false;
    }
    
    public function isRawHttpPost()
    {
        $param_name = 'Content-Type';
        $param_value = 'application/x-www-form-urlencoded';
        return ($this->isHttpPost() and ($this->getHeaderParam($param_name) != $param_value));
    }
    
    public function isHttpGet()
    {
        return ($this->getHttpMethod() == "GET") ? true : false;
    }
    
    public function isHttpPut()
    {
        return ($this->getHttpMethod() == "PUT") ? true : false;
    }
    
    public function isRawHttpPut()
    {
        $param_name = 'Content-Type';
        $param_value = 'application/x-www-form-urlencoded';
        return ($this->isHttpPut() and ($this->getHeaderParam($param_name) != $param_value));
    }
    
    public function isHttpDelete()
    {
        return ($this->getHttpMethod() == "DELETE") ? true : false;
    }
    
    public function isHttpHead()
    {
        return ($this->getHttpMethod() == "HEAD") ? true : false;
    }
    
    public function isHttpOption()
    {
        return ($this->getHttpMethod() == "OPTIONS") ? true : false;
    }
    
    public function getHeaderParam($param) 
    {
        $param = "HTTP_".strtoupper(str_replace('-', '_', $param));
        if (isset($_SERVER[$param])) {
            return $_SERVER[$param];
        }
        return null;
    }
}
